<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组</title>
</head>

<body>

</body>

</html>
<script>
   //数组的创建 字面量
    let arr = [1, 2, 3, '4', '哈哈']
    console.log(arr);
    //构造对象
    let arr2 = new Array('a', 'b')
    console.log(arr2);
    // 3、数组也可以先创建然后再添加值
    var arr3 = [];
    arr3[0] = 1;
    arr3[1] = "Tom";
    arr3[2] = "Lily"
    console.log(arr3);
    //数组长度
    let arr4 = ['牛嘉豪', '郭小儿', '周三金']
    arr4[arr4.length] = '余日天'
    console.log(arr4);
    //数组的判断
    let arr5 = [4, 5]
    console.log(Array. isArray(arr5));
    //数组元素添加
    let arr6 = ['牛嘉豪', '郭小儿', '周三金', '余日天']
    arr6.push('弓长土成光军')//添加末项，会改变原数组
    console.log(arr6);
    console.log('-----------------------------------');
    arr6.unshift('采花贼')
    console.log(arr6);//添加首项，会改变原数组
    //合并数组
    let arr7 = ['牛嘉豪', '郭小儿']
    let arr8 = ['周三金', '余日天']
    Array.prototype.push.apply(arr7, arr8)
    console.log(arr7);
    console.log(arr7.concat(arr8));
    console.log('-------------');
    console.log([...arr7, ...arr8]);
    //删除数组末尾项
    let arr9 = ['a', 'b', 'c', 'd', 'e']
    arr9.pop()//删除末尾，会改变原数组
    console.log(arr9);
    arr9.shift()
    console.log(arr9);//前面新增，会改变原数组
    //会改变原数组
    let arr10 = ['牛嘉豪', '郭小儿', '周三金', '余日天', '弓长土成光军']
    // arr10.splice(1) //从参数代表一直到结束删除
    // arr10.splice(1,2)//第一个参数代表开始的位置，第二个参数代表删除个数
    arr10.splice(1, 2, '采花贼')//第一个参数代表开始的位置，第二个参数代表删除个数,第三个参数代表删除后增加的值
    console.log(arr10);
    //数组截取 不会改变原数组
    let arr11 = [1, 2, 3, 4, 5]
    let res = arr11.slice(1, 3)
    console.log(res)
    // 数组排序
    let arr12 = [2, 5, 9, 6, 3, 7, 1, 8, 20, 10]
    console.log(arr12.sort());
    // 冒泡排序
    for (let i = 0; i < arr12.length - 1; i++) {
        for (let j = 0; j < arr12.length - 1 - i; j++) {
            if (arr12[j] > arr12[j + 1]) {
                let temp = arr12[j]//定义中间量接受最大值
                arr12[j] = arr12[j + 1]
                arr12[j + 1] = temp
            }
        }
    } console.log(arr12);
    // sort()函数方法使用
    let arr13 = [4, 5, 33, 9, 1, 8, 3, 2, 10, 7]
    console.log(arr13.sort());
    let newarr13 = arr13.sort(function (a, b) {
        return a - b//升序
        //return b-a//降序
    })
    //对象排序   {属性名：属性值}
    console.log(newarr13);
    let obj = {
        name: "张三",
        age: 18,
        sex: '男',
        job: '学生'
    }
    //JSDN 格式数据
    let stNam = [
        {
            name: '郭小儿',
            score: 70,
            age: 1
        },
        {
            name: '周三金',
            score: 80,
            age: 18
        },
        {
            name: '牛嘉豪',
            score: 60,
            age: 14
        }
    ]
    stNam.sort(function (a, b) {
        return a.age - b.age
    })
    console.log(stNam);
    //数组倒序
    let arr14 = [1, 4, 8, 3, 5, 2]
    console.log(arr14);
    console.log(arr14.sort().reverse());

    let heros = [1, 2, 3, 4];
    //- item`：每一个数组元素`index`：数组元素对应的下标，可选参数  `arr`：原始数组，可选参数
    let hreo = heros.map(function (item, index, arr) {
        //return结束循环返回值的作用
        return item * 2
        // return item >=4 ? item:''
    })
    console.log(hreo);
    //数组遍历
    let arr15 = [1, 2, 3, 4, 5, 6]
    for (let i = 0; i < arr15.length; i++) {
        console.log(arr15[i]);
    }
    //for in 循环  遍历的是下标
    for (index in arr15) {
        console.log(index, '------' + arr15[index]);
    }
    // for of 循环  直接遍历数据
    for (item of arr15) {
        console.log(item);
    }
    //forEach 遍历数据
    let sum = 0;
    arr15.forEach(function (item, index, arr) {
        console.log(item, index, arr);
        if (item % 2 == 0) {

            sum += item * 2
        } 
    })
    console.log(sum);

    let arr15new=[]
    arr15.forEach(function (item, index){
        if( item % 2 == 0){
         arr15new.push(item)
        }
    })
    console.log(arr15new);
    //过滤筛选
    let newArr15Back=arr15.filter(function(item, index, arr){
        // console.log(item, index, arr);
        return item > 2 && item < 5
    })
    console.log(newArr15Back);
    
    let students = [
        {name:'张三', sex:'男'},
        {name:'李四', sex:'女'},
        {name:'王五', sex:'女'},
        {name:'赵六', sex:'男'},
    ]
    let newstudents =students.filter(function(item){
        return item.sex=='男'
    })
    console.log(newstudents);

    //查询数组元素
    let arr16 = [1,'2','3','我','a']
    console.log(arr16.indexOf('2'));
    console.log(arr16.lastIndexOf('我'));
    console.log(arr16.includes(1));
    console.log(arr16.find(function(item){
        return item == 'a'
    }));
    console.log(arr16.findIndex(function(item){
        return item ==  '2'
    }));
    let arr16new=arr16.join('-')
    console.log(arr16new);
    //升序排列
    let str = 'a,c,b,k,z,9,g,3,5,8,2'
    let Newstr = str.split(',')
    console.log(Newstr.sort());
    let Nestr = Newstr.sort(function(a,b){
        return a-b
    })
    console.log(Nestr);
    console.log(Nestr.join(','));
    // 数组累加方法
    let arr17 = [22,44,66,45,77,88,98]
    let sum1=arr17.reduce(function(pre,cur,index,arr){
        return pre += cur
    },0)
    console.log(sum1);
    let sum2 = 0
    for(let i=0 ;i<arr17.length;i++){
        sum2 += arr17[i]
    }

    console.log(sum2);
    let commodity = [
        {name:'牛奶', prc:3},
        {name:'泡面', prc:5},
        {name:'可乐', prc:6},
        {name:'牙膏', prc:10},
    ]

    let sum4=commodity.reduce(function(pre,cur){
        return pre += cur.prc
    },0)
    console.log(`商品总价为:￥${sum4}`);
    // 数组去重
    let arr18 = [1,1,1,3,3,5,76,88,4,4,3,2,2]
    let Newarr18=arr18.reduce(function(pre,cur){
        if(!pre.includes(cur)){
            return pre.concat(cur) 
        }else{
            return pre
        }
    },[])
    console.log(Newarr18);
</script>